﻿/***********************************************************************
 * <copyright file="Mapper.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Tuanhm
 * Email:    Tuanhm@humg@gmail.com
 * Website:
 * Create Date: Thursday, March 27, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Collections.Generic;
using System.Linq;
using DataObjects.Shared;

namespace DataObjects.EntityFramework.ModelMapper
{
    /// <summary>
    /// Maps Entity Framework entities to business objects and vice versa.
    /// </summary>
    public static class Mapper
    {
        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.Customer Map(this Customer entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.Customer
            {
                CustomerId = (int)entity.CustomerID,
                UnitId = entity.UnitID,
                Code = entity.Code,
                Name = entity.Name,
                Address = entity.Address,
                PhoneNumber = entity.PhoneNumber,
                Fax = entity.Fax,
                CompanyName = entity.CompanyName,
                ContactName = entity.ContactName,
                Description = entity.Description,
                IsActive = entity.IsActive
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static Customer Map(this BusinessObjects.BussinessDictionary.Customer entity)
        {
            if (entity == null) return null;
            return new Customer
            {
                CustomerID = entity.CustomerId,
                UnitID = entity.UnitId,
                Code = entity.Code,
                Name = entity.Name,
                Address = entity.Address,
                PhoneNumber = entity.PhoneNumber,
                Fax = entity.Fax,
                CompanyName = entity.CompanyName,
                ContactName = entity.ContactName,
                Description = entity.Description,
                IsActive = entity.IsActive,
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.Customer> Map(this IEnumerable<Customer> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }


        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.Account Map(this Account entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.Account
            {
                //CustomerId = (int)entity.CustomerID,
                //UnitId = entity.UnitID,
                //Code = entity.Code,
                //Name = entity.Name,
                //Address = entity.Address,
                //PhoneNumber = entity.PhoneNumber,
                //Fax = entity.Fax,
                //CompanyName = entity.CompanyName,
                //ContactName = entity.ContactName,
                //Description = entity.Description,
                //IsActive = entity.IsActive,


                AccountId =(int) entity.AccountID,
                AccountCode = entity.AccountCode,
                AccountName = entity.AccountName,
                ParentId = entity.ParentID.AsInt(),
                Grade = entity.Grade.AsInt(),
                IsDetail = entity.IsDetail.AsBool(),
                Description = entity.Description,
                IsCustomer = entity.IsCustomer.AsBool(),
                IsVendor = entity.IsVendor.AsBool(),
                IsInventoryItem = entity.IsInventoryItem.AsBool(),
                IsFixedAsset = entity.IsFixedasset.AsBool(),
                IsTax = entity.IsTax.AsBool(),
                IsSystem = entity.IsSystem.AsBool(),
                IsActive = entity.IsActive.AsBool(),
                BalanceSide = entity.Balanceside.AsInt(),
                UnitId = entity.UnitID
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static Account Map(this BusinessObjects.BussinessDictionary.Account entity)
        {
            if (entity == null) return null;
            return new Account
            {
                AccountID = (int)entity.AccountId,
                AccountCode = entity.AccountCode,
                AccountName = entity.AccountName,
                ParentID = entity.ParentId.AsInt(),
                Grade = entity.Grade.AsInt(),
                IsDetail = entity.IsDetail.AsBool(),
                Description = entity.Description,
                IsCustomer = entity.IsCustomer.AsBool(),
                IsVendor = entity.IsVendor.AsBool(),
                IsInventoryItem = entity.IsInventoryItem.AsBool(),
                IsFixedasset = entity.IsFixedAsset.AsBool(),
                IsTax = entity.IsTax.AsBool(),
                IsSystem = entity.IsSystem.AsBool(),
                IsActive = entity.IsActive.AsBool(),
                Balanceside = entity.BalanceSide.AsInt(),
                UnitID = entity.UnitId
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.Account> Map(this IEnumerable<Account> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }


        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.Stock Map(this Stock entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.Stock
            {
                StockId = entity.StockID.AsId(),
                StockCode = entity.Code.AsString(),
                StockName = entity.Name.AsString(),
                Description = entity.Description.AsString(),
                IsActive = entity.IsActive.AsBool(),
                UnitId = entity.UnitID.AsLong()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static Stock Map(this BusinessObjects.BussinessDictionary.Stock entity)
        {
            if (entity == null) return null;
            return new Stock
            {
                StockID = entity.StockId.AsId(),
                Code = entity.StockCode.AsString(),
                Name = entity.StockName.AsString(),
                Description = entity.Description.AsString(),
                IsActive = entity.IsActive.AsBool(),
                UnitID = entity.UnitId.AsLong()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.Stock> Map(this IEnumerable<Stock> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }

        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.InventoryItem Map(this InventoryItem entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.InventoryItem
            {
                InventoryItemId = entity.InventoryItemID.AsId(),
                InventoryItemCode = entity.InventoryItemCode.AsString(),
                InventoryItemName = entity.InventoryItemName.AsString(),
                AccountCode = entity.AccountCode.AsString(),
                Unit = entity.Unit.AsString(),
                //   CurrencyCode = reader["CurrencyCode"].AsString(),
                //  CostMethod = reader["CostMethod"].AsInt(),
                IsActive = entity.IsActive.AsBool(),
                UnitId = entity.UnitID.AsLong()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static InventoryItem Map(this BusinessObjects.BussinessDictionary.InventoryItem entity)
        {
            if (entity == null) return null;
            return new InventoryItem
            {
                InventoryItemID = entity.InventoryItemId.AsId(),
                InventoryItemCode = entity.InventoryItemCode.AsString(),
                InventoryItemName = entity.InventoryItemName.AsString(),
                AccountCode = entity.AccountCode.AsString(),
                Unit = entity.Unit.AsString(),
                //   CurrencyCode = reader["CurrencyCode"].AsString(),
                //  CostMethod = reader["CostMethod"].AsInt(),
                IsActive = entity.IsActive.AsBool(),
                UnitID = entity.UnitId.AsLong()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.InventoryItem> Map(this IEnumerable<InventoryItem> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }

        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.AutoBusiness Map(this AutoBusiness entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.AutoBusiness
            {
                AutoBusinessId = entity.AutoBusinessID.AsInt(),
                AutoBusinessCode = entity.AutoBusinessCode.AsString(),
                AutoBusinessName = entity.AutoBusinessName.AsString(),
                RefTypeId = entity.RefTypeID.AsInt(),
                VoucherTypeId = entity.VoucherTypeID.AsInt(),
                DebitAccountNumber = entity.DebitAccountNumber.AsString(),
                CreditAccountNumber = entity.CreditAccountNumber.AsString(),
                Description = entity.Description.AsString(),
                IsActive = entity.IsActive.AsBool(),
                UnitId = entity.UnitID.AsLong()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static AutoBusiness Map(this BusinessObjects.BussinessDictionary.AutoBusiness entity)
        {
            if (entity == null) return null;
            return new AutoBusiness
            {
                AutoBusinessID = entity.AutoBusinessId.AsInt(),
                AutoBusinessCode = entity.AutoBusinessCode.AsString(),
                AutoBusinessName = entity.AutoBusinessName.AsString(),
                RefTypeID = entity.RefTypeId.AsInt(),
                VoucherTypeID = entity.VoucherTypeId.AsInt(),
                DebitAccountNumber = entity.DebitAccountNumber.AsString(),
                CreditAccountNumber = entity.CreditAccountNumber.AsString(),
                Description = entity.Description.AsString(),
                IsActive = entity.IsActive.AsBool(),
                UnitID = entity.UnitId.AsLong()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.AutoBusiness> Map(this IEnumerable<AutoBusiness> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }


        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.RefType Map(this RefType entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.RefType
            {
                RefTypeId = entity.RefTypeID.AsInt(),
                MasterTable = entity.MasterTable.AsString(),
                Name = entity.Name.AsString(),
                DetailTable = entity.DetailTable.AsString(),
                Description = entity.Description.AsString(),
                IsActive = entity.IsActive.AsBool()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static RefType Map(this BusinessObjects.BussinessDictionary.RefType entity)
        {
            if (entity == null) return null;
            return new RefType
            {
                RefTypeID = entity.RefTypeId.AsInt(),
                MasterTable = entity.MasterTable.AsString(),
                Name = entity.Name.AsString(),
                DetailTable = entity.DetailTable.AsString(),
                Description = entity.Description.AsString(),
                IsActive = entity.IsActive.AsBool()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.RefType> Map(this IEnumerable<RefType> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }


        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.VoucherType Map(this VoucherType entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.VoucherType
            {
                VoucherTypeId = entity.VoucherTypeID.AsInt(),
                VoucherTypeName = entity.VoucherTypeName.AsString(),
                IsActive = entity.IsActive.AsBool()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static VoucherType Map(this BusinessObjects.BussinessDictionary.VoucherType entity)
        {
            if (entity == null) return null;
            return new VoucherType
            {
                VoucherTypeID = entity.VoucherTypeId.AsInt(),
                VoucherTypeName = entity.VoucherTypeName.AsString(),
                IsActive = entity.IsActive.AsBool()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.VoucherType> Map(this IEnumerable<VoucherType> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }


        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.DbOption Map(this DBOption entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.DbOption
            {
                OptionId = entity.OptionID.AsId(),
                Code = entity.Code.AsString(),
                Description = entity.Description.AsString(),
                IsSystem = entity.IsSystem.AsBool(),
                Value = entity.Value.AsString(),
                ValueType = entity.ValueType.AsInt(),
                UnitId = entity.UnitID.AsInt()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static DBOption Map(this BusinessObjects.BussinessDictionary.DbOption entity)
        {
            if (entity == null) return null;
            return new DBOption
            {
                 OptionID= entity.OptionId.AsId(),
                Code = entity.Code.AsString(),
                Description = entity.Description.AsString(),
                IsSystem = entity.IsSystem.AsBool(),
                Value = entity.Value.AsString(),
                ValueType = entity.ValueType.AsInt(),
                UnitID = entity.UnitId.AsInt()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.DbOption> Map(this IEnumerable<DBOption> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }


        // <summary>
        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static BusinessObjects.BussinessDictionary.Unit Map(this Unit entity)
        {
            if (entity == null) return null;
            return new BusinessObjects.BussinessDictionary.Unit
            {
                UnitId = entity.UnitID.AsId(),
                Code = entity.Code.AsString(),
                Grade = entity.Grade.AsInt(),
                Address = entity.Address.AsString(),
                HasParent = entity.HasParent.AsBool(),
                ParentId = entity.ParentID.AsString().AsInteger(),

                Name = entity.Name.AsString(),
                IsActive = entity.IsActive.AsBool(),
                PhoneNumber = entity.PhoneNumber.AsString(),
                ByManagement = entity.ByManagement.AsString()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static Unit Map(this BusinessObjects.BussinessDictionary.Unit entity)
        {
            if (entity == null) return null;
            return new Unit
            {
                UnitID = entity.UnitId.AsId(),
                Code = entity.Code.AsString(),
                Grade = entity.Grade.AsInt(),
                Address = entity.Address.AsString(),
                HasParent = entity.HasParent.AsBool(),
                ParentID = entity.ParentId.AsString().AsInteger(),

                Name = entity.Name.AsString(),
                IsActive = entity.IsActive.AsBool(),
                PhoneNumber = entity.PhoneNumber.AsString(),
                ByManagement = entity.ByManagement.AsString()
            };
        }

        /// <summary>
        /// Maps the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static IList<BusinessObjects.BussinessDictionary.Unit> Map(this IEnumerable<Unit> entity)
        {
            return entity == null ? null : entity.Select(source => Map((source))).ToList();
        }

    }
}
